package com.lrkj.back.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.StartUpOnLoadData;
import com.lrkj.back.entity.TbPackageMuban;
import com.lrkj.back.entity.ZsPackageImformation;
import com.lrkj.back.entity.ZsQingxi;
import com.lrkj.back.entity.ZsWanglanpackage;
import com.lrkj.back.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lrkj.back.Const;
import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.github.pagehelper.PageInfo;
@Controller
@RequestMapping("/zhuisuPackage")
public class ZszhuisuxitongController extends BaseController{
	@Autowired
	private ZsYicipinRukuService yicipinService;
	@Autowired
	private ZsShenqingService shenqingService;
	@Autowired
	private TbManufactorService manufactorService;
	@Autowired
	private ZsShenqingYicipinMingxiService sqmxService;
	
	@Autowired
	private TbSeqService seqService;
	@Autowired
	private ZsShenqingShoushubaoMingxiService ssbservice;

	@Autowired
	private TbDepartmentService departmentService;

	@Autowired
	private ZsYicipinKucunService kucunService;
	@Autowired
	private TbPackageMubanService mubanservice;
	@Autowired
	private TbSeqService  sepservice;
	@Autowired
	private ZsWupinBaoFafangService  baofafangservice;
	@Autowired
	private ZsWupinYcpFafangService  yicipinfafangservice;
	@Autowired
	private ZsWupinFafangService   wupingfafangservice;
	@Autowired
	private TbPackageMubanService   ssbmubanservice;
	@Autowired
	private TbPackageProductService ssbxiangxiservice;
	@Autowired
	private TbProductService productservice;
	@Autowired
	private TbPackageProductService baoqixieservice;
	@Autowired
	private ZsDabaoService daobaoservice;
	@Autowired
	private TbEmployeeService employeservice;
	@Autowired
	private ZsPackageImformationService zsPackageInformationService;

	@Autowired
	private ZsPackageImformationService packageInfomationservice;
	@Autowired
	private ZsEquipService  equipservice;
	@Autowired
	private ZsSoftService  softservice;
	@Autowired
	private ZsWanglanpackageService zsWanglanpackageService;

	@Autowired
	private ZsQingxiService zsQingxiService;
	
	//物品发放--物品追溯功能  （点击查询）
	@RequestMapping("/packageingList.do")
	public ModelAndView packageingList(String searchTxt,HttpServletRequest request,
			HttpServletResponse response) throws BusinessException,ParseException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<>();
		String shijianleixin=request.getParameter("leixin");
		String startTime=request.getParameter("startTime");
		String endTime=request.getParameter("endTime");
		SimpleDateFormat datezhuanhuan=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//		searchTxt="+ $(".search_text").val()+"&leixin"+shijianleixin+"&startTime="+starttime+"&endTime="+endtime;;
		Date kaishishijian=null;
		Date jieshushijian=null;
		if(StringUtils.isNotBlank(searchTxt)){
			map.put("searchTxt", searchTxt.trim());
		}
		if(StringUtils.isNotBlank(shijianleixin)){
			map.put("sjlx", shijianleixin);
		}
		if(StringUtils.isNotBlank(startTime)){
			
			kaishishijian=datezhuanhuan.parse(startTime);
			map.put("startTime", kaishishijian);
		}
		if(StringUtils.isNotBlank(endTime)){
			jieshushijian=datezhuanhuan.parse(endTime);
			map.put("endTime", jieshushijian);
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<ZsPackageImformation> page = packageInfomationservice.findAllInformation(map);
//		List<ZsPackageImformation> lllList = page.getList();
		//FIXME 查到包的功能 ,只展示一个包的信息
		/*if(page.getSize()>0){
			try{
			    TbPackageMuban baoMuban=ssbmubanservice.selectByPrimaryKey(page.getList().get(0).getPackageId());
				if(baoMuban!=null){
				String gongneng=baoMuban.getPackageFunction();
				String departmentname=departmentService.selectByPrimaryKey(baoMuban.getPackageDepartid()).getDepartmentName();
			}
		    Map<String, Object> map2 = new HashMap<String,Object>();
		    map2.put("equipNo", page.getList().get(0).getQingxiShebeiTiaoma());
			}catch(Exception e){
				e.printStackTrace();
			}
		}*/
		request.setAttribute("dabaoList", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		request.setAttribute("empmap", StartUpOnLoadData.emMap);
		request.setAttribute("equipmap", StartUpOnLoadData.equipMap);
		request.setAttribute("softmap", StartUpOnLoadData.softMap);
		return new ModelAndView("/wupinfafang/zhuisugongneng/zhuisubao");
	}




	@RequestMapping("/shoushuList")
	public ModelAndView shoushuList(){
		return new ModelAndView("/zhuisugongneng/flowNotice");
	}
	
	@RequestMapping("/huishouwanglan.do")
	public ModelAndView wanglanbao(String searchTxt,String huishouwl,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String, Object> map = new HashMap<>();
		map.put("wanglanTiaoma", huishouwl);
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<ZsWanglanpackage> page = zsWanglanpackageService.findAllWlpackList(map);
		request.setAttribute("huishoubao", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		return new ModelAndView("/wupinfafang/zhuisugongneng/huishou_bao");
	}
	
	@RequestMapping("/qingxibao.do")
	public ModelAndView qingxibao(String searchTxt,String qingxibao,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String, Object> map = new HashMap<>();
		map.put("qingxiLiushui", qingxibao);
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<ZsWanglanpackage> page = zsWanglanpackageService.findAllWlpackList(map);
		request.setAttribute("qingxibao", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		return new ModelAndView("/wupinfafang/zhuisugongneng/qingxi_bao");
	}

	@RequestMapping("/liucheng")
	public ModelAndView liucheng(String searchTxt,String qingxibao,HttpServletRequest request,
								  HttpServletResponse response)throws BusinessException{
		return new ModelAndView("/system/liuchengs");
	}
	
	@RequestMapping("/miejunbao.do")
	public ModelAndView miejunbao(String searchTxt,String miejunbao,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("miejunLiushui", miejunbao);
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<ZsPackageImformation> page = zsPackageInformationService.findAllInformation(map);
		request.setAttribute("miejunbao", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		return new ModelAndView("/wupinfafang/zhuisugongneng/miejun_bao");
	}

	//获得当天0点时间
	public static Date getTimesmorning(){
		Date date = new Date();
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		return date;
	}
	//获得当天24点时间
	public static Date getTimesnight(){
		Date date = new Date();
		date.setHours(23);
		date.setMinutes(59);
		date.setSeconds(59);
		return date;
	}

	@RequestMapping("/findPackList")
	@ResponseBody
	public Map<String,Object> findPackList(String searchTxt ,String time) {
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> rtnMap = new HashMap<>();
		List<ZsPackageImformation> list = new ArrayList<>();
		String[] times = new String[2];
		Date startTime = new Date();
		Date endTime = new Date();
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
		try {
			if (StringUtils.isNotBlank(time)) {
//				char s = time.charAt(9);
//				String c = String.valueOf(s);
				times =time.split("一");
				if(times.length==2){
					StringBuffer stringBuffer = new StringBuffer(times[0]);
					StringBuffer stringBuffer2 = new StringBuffer(times[1]);
//					times[0] = String.valueOf(stringBuffer.insert(times[0].indexOf("/")+1,"0"));
//					times[1] = String.valueOf(stringBuffer2.insert(times[1].indexOf("/")+1,"0"));
					startTime = sdf2.parse(times[0]);
					endTime = sdf2.parse(times[1]);
					long timeNum  = endTime.getTime()+24l*60*60*1000;
					endTime.setTime(timeNum);
				}
			}else{
				startTime = getTimesmorning();
				endTime = getTimesnight();

			}
			if (searchTxt.length() == 11 && searchTxt.startsWith("R")) {
				ZsPackageImformation zsPackageImformation = zsPackageInformationService.selectByPrimaryKey(searchTxt);
				if(zsPackageImformation!=null){
					TbPackageMuban tbPackageMuban = zsPackageImformation.getTbPackageMuban();
					if(tbPackageMuban!=null){
						Integer outPackage = tbPackageMuban.getPackageOutcategory();
						if(outPackage!=null){
							if(outPackage==1){
								List<ZsWanglanpackage> zsWanglanpackages = zsWanglanpackageService.selectByPackageTiaoma(zsPackageImformation.getPackageTiaoma());
								String qingxiLiushui = "";
								String qingSpit = zsWanglanpackages.get(0).getQingxiLiushui().substring(2);
								try {
									for(ZsWanglanpackage zsWanglanpackage:zsWanglanpackages){
										String test = zsWanglanpackage.getQingxiLiushui().substring(2);
										qingSpit= Long.valueOf(qingSpit)>=Long.valueOf(test)?qingSpit:test;

									}
								}catch (Exception e){
									e.printStackTrace();
								}

								qingxiLiushui = "QX"+qingSpit;
								ZsQingxi zsQingxi = zsQingxiService.selectByPrimaryKey(qingxiLiushui);
								if(zsWanglanpackages.size()>1){
									zsPackageImformation.setZsQingxi(zsQingxi);
								}


							}
						}
					}
				}else{
					rtnMap.put("msg","暂无信息");
					rtnMap.put("status",false);
					return rtnMap;
				}

				if (zsPackageImformation != null) {
					list.add(zsPackageImformation);
				}
			} else {
				map.put("startTime", startTime);
				map.put("endTime", endTime);
				map.put("packageName", searchTxt);
				list = zsPackageInformationService.selectSin(map);
				for(ZsPackageImformation zsPackageImformation:list){
					if(zsPackageImformation!=null){
						TbPackageMuban tbPackageMuban = zsPackageImformation.getTbPackageMuban();
						if(tbPackageMuban!=null){
							Integer outPackage = tbPackageMuban.getPackageOutcategory();
							if(outPackage!=null){
								if(outPackage==1){
									List<ZsWanglanpackage> zsWanglanpackages = zsWanglanpackageService.selectByPackageTiaoma(zsPackageImformation.getPackageTiaoma());
									String qingxiLiushui = "";
									String qingSpit = zsWanglanpackages.get(0).getQingxiLiushui().substring(2);
									try {
										for(ZsWanglanpackage zsWanglanpackage:zsWanglanpackages){
											String test = zsWanglanpackage.getQingxiLiushui().substring(2);
											qingSpit= Long.valueOf(qingSpit)>=Long.valueOf(test)?qingSpit:test;

										}
									}catch (Exception e){

									}

									qingxiLiushui = "QX"+qingSpit;
									ZsQingxi zsQingxi = zsQingxiService.selectByPrimaryKey(qingxiLiushui);
									if(zsWanglanpackages.size()>1){
										zsPackageImformation.setZsQingxi(zsQingxi);
									}
								}
							}
						}
					}
				}

			}
			rtnMap.put("packageList", list);
			rtnMap.put("msg", "查询成功");
			rtnMap.put("empMap",StartUpOnLoadData.emMap);
			rtnMap.put("equipmap",StartUpOnLoadData.equipSinMap);
			rtnMap.put("softmap",StartUpOnLoadData.softSinMap);
			rtnMap.put("depMap",StartUpOnLoadData.depMap);
			rtnMap.put("status", true);
			return rtnMap;

		}catch (NullPointerException e){
			rtnMap.put("packageList", list);
			rtnMap.put("msg", "暂无信息");
			rtnMap.put("status", false);
			rtnMap.put("empMap",StartUpOnLoadData.emMap);
			rtnMap.put("equipmap",StartUpOnLoadData.equipSinMap);
			rtnMap.put("softmap",StartUpOnLoadData.softSinMap);
			rtnMap.put("depMap",StartUpOnLoadData.depMap);
			e.printStackTrace();
		}catch(Exception e){
			rtnMap.put("packageList", list);
			rtnMap.put("msg", "查询失败");
			rtnMap.put("status", false);
			rtnMap.put("empMap",StartUpOnLoadData.emMap);
			rtnMap.put("equipmap",StartUpOnLoadData.equipSinMap);
			rtnMap.put("softmap",StartUpOnLoadData.softSinMap);
			rtnMap.put("depMap",StartUpOnLoadData.depMap);
			e.printStackTrace();
		}
		return rtnMap;
	}
}
